package models

import (
	"go-scripts/common"
	"gopkg.in/mgo.v2"
)

type Model struct {
	mongo *common.DB
}

const (
	tmpInfo = "TmpInfo"
	theme = "Theme"
	user = "User"
	feedback = "Feedback"
	effectTopic = "EffectTopic"
	themePoint = "ThemePoint"
	tmpNews = "TmpNews"
	stock = "Stock"
	topInfo = "TopInfo"
)

var modelInstance *Model = nil

func GetModel() *Model {
	if modelInstance != nil {
		return modelInstance
	}
	modelInstance = new(Model)
	modelInstance.mongo = common.GetMongo()
	return modelInstance
}

func (m *Model) GetModelCollection(modelName string) (*mgo.Collection, error) {
	var name string = ""
	var db string = ""
	switch modelName {
	case tmpInfo:
		name = "tmp_info"
		db = "abnormal"
		break
	case theme:
		name = "theme"
		db = "mongo"
		break
	case user:
		name = "user"
		db = "mongo"
	case feedback:
		name = "feedback"
		db = "mongo"
	case effectTopic:
		name = "effect_topic"
		db = "mongo"
	case themePoint:
		name = "theme_point"
		db = "mongo"
	case tmpNews:
		name = "tmp_news"
		db = "abnormal"
	case stock:
		name = "stock"
		db = "mongo"
	case topInfo:
		name = "top_info"
		db = "mongo"
	default:
		name = ""
		db = ""
	}
	if name == "" {
		return nil, common.NewError("model 不存在", common.ModelError)
	}
	if db == "" {
		return nil, common.NewError("db 不存在", common.MongoError)
	}
	var collection *mgo.Collection
	if db == "mongo" {
		collection = m.mongo.GetMongoCollection(name)
	}
	if db == "abnormal" {
		collection = m.mongo.GetAbnormalCollection(name)
	}
	return collection, nil
}